﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Systems.Bussiness.Template;
using Portal.Web.Utility;
using Portal.Core;
using Portal.Utility;
using System.Collections;
using System.Reflection;

namespace Portal.Web.Utility
{
    public class SystemBussinessUtility
    {
        static ISystemsBussinessProcess[] LoadAssembly(string filename)
        {
            ArrayList ArrayListModules = new ArrayList();
            Assembly a = Assembly.LoadFrom(filename);
            if (a == null) throw new Exception("Assembly " + filename + " not found or not valid");
            foreach (Module mod in a.GetLoadedModules())
            {
                foreach (Type ty in mod.GetTypes())
                {
                    foreach (Type intf in ty.GetInterfaces())
                    {
                        if (intf == typeof(ISystemsBussinessProcess))
                        {
                            ISystemsBussinessProcess w = (ISystemsBussinessProcess)a.CreateInstance(ty.FullName);
                            ArrayListModules.Add(w);
                        }
                    }
                }
            }
            ISystemsBussinessProcess[] ws = new ISystemsBussinessProcess[ArrayListModules.Count];
            for (int iIndex = 0; iIndex < ArrayListModules.Count; iIndex++)
            {
                ws[iIndex] = (ISystemsBussinessProcess)ArrayListModules[iIndex];
            }
            return ws;
        }


        static ISystemsBussinessProcess[] LoadPlugIn(SiteParam OSiteParam)
        {
            string Path = OSiteParam.DllPath;
            string[] Files = System.IO.Directory.GetFiles(Path, "*.dll");
            ArrayList ArrayListServives = new ArrayList();
            for (int iIndex = 0; iIndex < Files.Length; iIndex++)
            {
                try
                {
                    string FileDll = Files[iIndex];
                    ISystemsBussinessProcess[]
                        asms = LoadAssembly(FileDll);
                    for (int jIndex = 0; jIndex < asms.Length; jIndex++)
                    {
                        ArrayListServives.Add(asms[jIndex]);
                    }
                }
                catch (Exception ex)
                {
                    ex.Source = "";
                }
            }
            ISystemsBussinessProcess[] WebParts = new ISystemsBussinessProcess[ArrayListServives.Count];
            for (int iIndex = 0; iIndex < ArrayListServives.Count; iIndex++)
            {
                WebParts[iIndex] = (ISystemsBussinessProcess)ArrayListServives[iIndex];
            }

            return WebParts;
        }


        static ISystemsBussinessProcess Find(SiteParam OSiteParam, string ServiceId)
        {
            ISystemsBussinessProcess[] found = LoadPlugIn(OSiteParam);
            for (int iIndex = 0; iIndex < found.Length; iIndex++)
            {
                if (found[iIndex].ServiceId != null)
                {
                    if (found[iIndex].ServiceId.Trim().ToLower().Equals(ServiceId.Trim().ToLower()))
                    {
                        return found[iIndex];
                    }
                }
            }
            return null;
        }


        public static ISystemsBussinessProcess CreateSystemsBussinessProcess(string SiteId)
        {
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string ServiceId=WebConfig.GetPortalSystemsBussinessService(OSiteParam);
            if (string.IsNullOrEmpty(ServiceId))
                ServiceId = "Portal.Systems.Bussiness.dll";

            ISystemsBussinessProcess Found = Find(OSiteParam, ServiceId);
            if (Found == null) throw new Exception("Không tìm thấy dịch vụ " + ServiceId);
            return Found;
        }
    }
}
